<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"always","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="本文部分摘录自网道 JavaScript 教程，仅供学习参考参考地址：https:&#x2F;&#x2F;wangdoc.com&#x2F;javascript&#x2F;oop&#x2F;prototype.html#prototype-%E5%B1%9E%E6%80%A7%E7%9A%84%E4%BD%9C%E7%94%A8  prototype 属性的作用JavaScript 继承机制的设计思想就是，原型对象的所有属性和方法，都能被实例对">
<meta property="og:type" content="article">
<meta property="og:title" content="原型，原型链">
<meta property="og:url" content="http://yoursite.com/2016/07/21/%E5%8E%9F%E5%9E%8B%EF%BC%8C%E5%8E%9F%E5%9E%8B%E9%93%BE/index.html">
<meta property="og:site_name" content="Albert Guo">
<meta property="og:description" content="本文部分摘录自网道 JavaScript 教程，仅供学习参考参考地址：https:&#x2F;&#x2F;wangdoc.com&#x2F;javascript&#x2F;oop&#x2F;prototype.html#prototype-%E5%B1%9E%E6%80%A7%E7%9A%84%E4%BD%9C%E7%94%A8  prototype 属性的作用JavaScript 继承机制的设计思想就是，原型对象的所有属性和方法，都能被实例对">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://inews.gtimg.com/newsapp_ls/0/12123167721/0">
<meta property="og:image" content="https://inews.gtimg.com/newsapp_ls/0/12123168822/0">
<meta property="article:published_time" content="2016-07-21T05:50:51.000Z">
<meta property="article:modified_time" content="2020-07-21T06:01:50.150Z">
<meta property="article:author" content="Albert Guo">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://inews.gtimg.com/newsapp_ls/0/12123167721/0">

<link rel="canonical" href="http://yoursite.com/2016/07/21/%E5%8E%9F%E5%9E%8B%EF%BC%8C%E5%8E%9F%E5%9E%8B%E9%93%BE/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>原型，原型链 | Albert Guo</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Albert Guo</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-bars fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-address-card fa-fw"></i>简介</a>

  </li>
        <li class="menu-item menu-item-webpack">

    <a href="/webpack/" rel="section"><i class="fa fa-bookmark fa-fw"></i>webpack</a>

  </li>
        <li class="menu-item menu-item-javascript">

    <a href="/JavaScript/" rel="section"><i class="fa fa-bookmark fa-fw"></i>ECMAScript 5.1</a>

  </li>
        <li class="menu-item menu-item-ecmascript6">

    <a href="/ECMAScript6/" rel="section"><i class="fa fa-bookmark fa-fw"></i>ECMAScript 6</a>

  </li>
        <li class="menu-item menu-item-react">

    <a href="/React/" rel="section"><i class="fa fa-bookmark fa-fw"></i>React</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2016/07/21/%E5%8E%9F%E5%9E%8B%EF%BC%8C%E5%8E%9F%E5%9E%8B%E9%93%BE/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://inews.gtimg.com/newsapp_ls/0/12122532695/0">
      <meta itemprop="name" content="Albert Guo">
      <meta itemprop="description" content="郭鹏松的个人博客">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Albert Guo">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          原型，原型链
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2016-07-21 13:50:51" itemprop="dateCreated datePublished" datetime="2016-07-21T13:50:51+08:00">2016-07-21</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-07-21 14:01:50" itemprop="dateModified" datetime="2020-07-21T14:01:50+08:00">2020-07-21</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/JavaScript/" itemprop="url" rel="index"><span itemprop="name">JavaScript</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <blockquote>
<p>本文部分摘录自网道 JavaScript 教程，仅供学习参考<br>参考地址：<a href="https://wangdoc.com/javascript/oop/prototype.html#prototype-%E5%B1%9E%E6%80%A7%E7%9A%84%E4%BD%9C%E7%94%A8" target="_blank" rel="noopener">https://wangdoc.com/javascript/oop/prototype.html#prototype-%E5%B1%9E%E6%80%A7%E7%9A%84%E4%BD%9C%E7%94%A8</a></p>
</blockquote>
<h1 id="prototype-属性的作用"><a href="#prototype-属性的作用" class="headerlink" title="prototype 属性的作用"></a>prototype 属性的作用</h1><p>JavaScript 继承机制的设计思想就是，原型对象的所有属性和方法，都能被实例对象共享。也就是说，如果属性和方法定义在原型上，那么所有实例对象就能共享，不仅节省了内存，还体现了实例对象之间的联系。</p>
<p>下面，先看怎么为对象指定原型。JavaScript 规定，每个函数都有一个 <code>prototype</code> 属性，指向一个对象。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">function f() &#123;&#125;</span><br><span class="line">typeof f.prototype &#x2F;&#x2F; &quot;object&quot;</span><br></pre></td></tr></table></figure>

<p>上面代码中，函数f默认具有 <code>prototype</code> 属性，指向一个对象。</p>
<p>对于普通函数来说，该属性基本无用。但是，对于构造函数来说，生成实例的时候，该属性会自动成为实例对象的原型。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">function Animal(name) &#123;</span><br><span class="line">  this.name &#x3D; name;</span><br><span class="line">&#125;</span><br><span class="line">Animal.prototype.color &#x3D; &#39;white&#39;;</span><br><span class="line"></span><br><span class="line">var cat1 &#x3D; new Animal(&#39;大毛&#39;);</span><br><span class="line">var cat2 &#x3D; new Animal(&#39;二毛&#39;);</span><br><span class="line"></span><br><span class="line">cat1.color &#x2F;&#x2F; &#39;white&#39;</span><br><span class="line">cat2.color &#x2F;&#x2F; &#39;white&#39;</span><br></pre></td></tr></table></figure>

<p>上面代码中，构造函数 <code>Animal</code> 的 <code>prototype</code> 属性，就是实例对象 <code>cat1</code> 和 <code>cat2</code> 的原型对象。原型对象上添加一个 <code>color</code> 属性，结果，实例对象都共享了该属性。</p>
<p>原型对象的属性不是实例对象自身的属性。只要修改原型对象，变动就立刻会体现在所有实例对象上。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Animal.prototype.color &#x3D; &#39;yellow&#39;;</span><br><span class="line"></span><br><span class="line">cat1.color &#x2F;&#x2F; &quot;yellow&quot;</span><br><span class="line">cat2.color &#x2F;&#x2F; &quot;yellow&quot;</span><br></pre></td></tr></table></figure>

<p>上面代码中，原型对象的 <code>color</code> 属性的值变为 <code>yellow</code> ，两个实例对象的 <code>color</code> 属性立刻跟着变了。这是因为实例对象其实没有 <code>color</code> 属性，都是读取原型对象的 <code>color</code> 属性。也就是说，==当实例对象本身没有某个属性或方法的时候，它会到原型对象去寻找该属性或方法。== 这就是原型对象的特殊之处。</p>
<p>==如果实例对象自身就有某个属性或方法，它就不会再去原型对象寻找这个属性或方法。==</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">cat1.color &#x3D; &#39;black&#39;;</span><br><span class="line"></span><br><span class="line">cat1.color &#x2F;&#x2F; &#39;black&#39;</span><br><span class="line">cat2.color &#x2F;&#x2F; &#39;yellow&#39;</span><br><span class="line">Animal.prototype.color &#x2F;&#x2F; &#39;yellow&#39;;</span><br></pre></td></tr></table></figure>

<p>上面代码中，实例对象 <code>cat1</code> 的 <code>color</code> 属性改为 <code>black</code>，就使得它不再去原型对象读取 <code>color</code> 属性，后者的值依然为 <code>yellow</code> 。</p>
<p>总结一下，==原型对象的作用，就是定义所有实例对象共享的属性和方法。== 这也是它被称为原型对象的原因，而实例对象可以视作从原型对象衍生出来的子对象。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Animal.prototype.walk &#x3D; function () &#123;</span><br><span class="line">  console.log(this.name + &#39; is walking&#39;);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>上面代码中，<code>Animal.prototype</code> 对象上面定义了一个walk方法，这个方法将可以在所有 <code>Animal</code> 实例对象上面调用。</p>
<p><img src="https://inews.gtimg.com/newsapp_ls/0/12123167721/0" alt="原型示意图"></p>
<h1 id="原型链"><a href="#原型链" class="headerlink" title="原型链"></a>原型链</h1><p>==JavaScript 规定，所有对象都有自己的原型对象（<code>prototype</code>）。一方面，任何一个对象，都可以充当其他对象的原型；另一方面，由于原型对象也是对象，所以它也有自己的原型。因此，就会形成一个“原型链”：对象到原型，再到原型的原型……==</p>
<p>如果一层层地上溯，所有对象的原型最终都可以上溯到 <code>Object.prototype</code>，即 <code>Object</code> 构造函数的 <code>prototype</code> 属性。也就是说，所有对象都继承了 <code>Object.prototype</code> 的属性。这就是所有对象都有 <code>valueOf</code> 和 <code>toString</code> 方法的原因，因为这是从 <code>Object.prototype</code> 继承的。</p>
<p><code>Object.prototype</code> 的原型是 <code>null</code> 。<code>null</code> 没有任何属性和方法，也没有自己的原型。因此，==原型链的尽头就是 <code>null</code>。==</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Object.getPrototypeOf(Object.prototype)</span><br><span class="line">&#x2F;&#x2F; null</span><br></pre></td></tr></table></figure>

<p>上面代码表示，<code>Object.prototype</code> 对象的原型是 <code>null</code>，由于 <code>null</code> 没有任何属性，所以原型链到此为止。<code>Object.getPrototypeOf</code> 方法返回参数对象的原型。</p>
<p>==读取对象的某个属性时，JavaScript  引擎先寻找对象本身的属性，如果找不到，就到它的原型去找，如果还是找不到，就到原型的原型去找。如果直到最顶层的 <code>Object.prototype</code> 还是找不到，则返回 <code>undefined</code> 。如果对象自身和它的原型，都定义了一个同名属性，那么优先读取对象自身的属性，这叫做“覆盖”。==</p>
<p>注意，一级级向上，在整个原型链上寻找某个属性，对性能是有影响的。所寻找的属性在越上层的原型对象，对性能的影响越大。如果寻找某个不存在的属性，将会遍历整个原型链。</p>
<p>举例来说，如果让构造函数的 <code>prototype</code> 属性指向一个数组，就意味着实例对象可以调用数组方法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">var MyArray &#x3D; function () &#123;&#125;;</span><br><span class="line"></span><br><span class="line">MyArray.prototype &#x3D; new Array();</span><br><span class="line">MyArray.prototype.constructor &#x3D; MyArray;</span><br><span class="line"></span><br><span class="line">var mine &#x3D; new MyArray();</span><br><span class="line">mine.push(1, 2, 3);</span><br><span class="line">mine.length &#x2F;&#x2F; 3</span><br><span class="line">mine instanceof Array &#x2F;&#x2F; true</span><br></pre></td></tr></table></figure>

<p>上面代码中，<code>mine</code> 是构造函数 <code>MyArray</code> 的实例对象，由于 <code>MyArray.prototype</code> 指向一个数组实例，使得mine可以调用数组方法（这些方法定义在数组实例的 <code>prototype</code> 对象上面）。最后那行 <code>instanceof</code> 表达式，用来比较一个对象是否为某个构造函数的实例，结果就是证明 <code>mine</code> 为 <code>Array</code> 的实例。</p>
<p><img src="https://inews.gtimg.com/newsapp_ls/0/12123168822/0" alt="原型链示意图"></p>
<h1 id="constructor-属性"><a href="#constructor-属性" class="headerlink" title="constructor 属性"></a>constructor 属性</h1><p>==<code>prototype</code> 对象有一个 <code>constructor</code> 属性，默认指向 <code>prototype</code> 对象所在的构造函数。==</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">function P() &#123;&#125;</span><br><span class="line">P.prototype.constructor &#x3D;&#x3D;&#x3D; P &#x2F;&#x2F; true</span><br></pre></td></tr></table></figure>

<p>由于 <code>constructor</code> 属性定义在 <code>prototype</code> 对象上面，意味着可以被所有实例对象继承。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">function P() &#123;&#125;</span><br><span class="line">var p &#x3D; new P();</span><br><span class="line"></span><br><span class="line">p.constructor &#x3D;&#x3D;&#x3D; P &#x2F;&#x2F; true</span><br><span class="line">p.constructor &#x3D;&#x3D;&#x3D; P.prototype.constructor &#x2F;&#x2F; true</span><br><span class="line">p.hasOwnProperty(&#39;constructor&#39;) &#x2F;&#x2F; false</span><br></pre></td></tr></table></figure>

<p>上面代码中，<code>p</code> 是构造函数 <code>P</code> 的实例对象，但是 <code>p</code> 自身没有 <code>constructor</code> 属性，该属性其实是读取原型链上面的 <code>P.prototype.constructor</code> 属性。</p>
<p><code>constructor</code> 属性的作用是，可以得知某个实例对象，到底是哪一个构造函数产生的。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">function F() &#123;&#125;;</span><br><span class="line">var f &#x3D; new F();</span><br><span class="line"></span><br><span class="line">f.constructor &#x3D;&#x3D;&#x3D; F &#x2F;&#x2F; true</span><br><span class="line">f.constructor &#x3D;&#x3D;&#x3D; RegExp &#x2F;&#x2F; false</span><br></pre></td></tr></table></figure>

<p>上面代码中，<code>constructor</code> 属性确定了实例对象f的构造函数是 <code>F</code>，而不是 <code>RegExp</code>。</p>
<p>另一方面，有了 <code>constructor</code> 属性，就可以从一个实例对象新建另一个实例。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">function Constr() &#123;&#125;</span><br><span class="line">var x &#x3D; new Constr();</span><br><span class="line"></span><br><span class="line">var y &#x3D; new x.constructor();</span><br><span class="line">y instanceof Constr &#x2F;&#x2F; true</span><br></pre></td></tr></table></figure>

<p>上面代码中，<code>x</code> 是构造函数 <code>Constr</code> 的实例，可以从 <code>x.constructor</code> 间接调用构造函数。这使得在实例方法中，调用自身的构造函数成为可能。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Constr.prototype.createCopy &#x3D; function () &#123;</span><br><span class="line">  return new this.constructor();</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>上面代码中，<code>createCopy</code> 方法调用构造函数，新建另一个实例。</p>
<p><code>constructor</code> 属性表示原型对象与构造函数之间的关联关系，如果修改了原型对象，一般会同时修改 <code>constructor</code> 属性，防止引用的时候出错。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">function Person(name) &#123;</span><br><span class="line">  this.name &#x3D; name;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Person.prototype.constructor &#x3D;&#x3D;&#x3D; Person &#x2F;&#x2F; true</span><br><span class="line"></span><br><span class="line">Person.prototype &#x3D; &#123;</span><br><span class="line">  method: function () &#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">Person.prototype.constructor &#x3D;&#x3D;&#x3D; Person &#x2F;&#x2F; false</span><br><span class="line">Person.prototype.constructor &#x3D;&#x3D;&#x3D; Object &#x2F;&#x2F; true</span><br></pre></td></tr></table></figure>

<p>上面代码中，构造函数 <code>Person</code> 的原型对象改掉了，但是没有修改 <code>constructor</code> 属性，导致这个属性不再指向 <code>Person</code>。由于 <code>Person</code> 的新原型是一个普通对象，而普通对象的 <code>constructor</code> 属性指向 <code>Object</code> 构造函数，导致 <code>Person.prototype.constructor</code> 变成了 <code>Object</code>。</p>
<p>所以，修改原型对象时，一般要同时修改 <code>constructor</code> 属性的指向。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 坏的写法</span><br><span class="line">C.prototype &#x3D; &#123;</span><br><span class="line">  method1: function (...) &#123; ... &#125;,</span><br><span class="line">  &#x2F;&#x2F; ...</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 好的写法</span><br><span class="line">C.prototype &#x3D; &#123;</span><br><span class="line">  constructor: C,</span><br><span class="line">  method1: function (...) &#123; ... &#125;,</span><br><span class="line">  &#x2F;&#x2F; ...</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; 更好的写法</span><br><span class="line">C.prototype.method1 &#x3D; function (...) &#123; ... &#125;;</span><br></pre></td></tr></table></figure>

<p>上面代码中，要么将 <code>constructor</code> 属性重新指向原来的构造函数，要么只在原型对象上添加方法，这样可以保证 <code>instanceof</code> 运算符不会失真。</p>
<p>如果不能确定 <code>constructor</code> 属性是什么函数，还有一个办法：通过 <code>name</code> 属性，从实例得到构造函数的名称。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">function Foo() &#123;&#125;</span><br><span class="line">var f &#x3D; new Foo();</span><br><span class="line">f.constructor.name &#x2F;&#x2F; &quot;Foo&quot;</span><br></pre></td></tr></table></figure>




    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item"></div>
      <div class="post-nav-item">
    <a href="/2016/09/15/Javascript%E5%BC%82%E6%AD%A5%E7%BC%96%E7%A8%8B%E7%9A%844%E7%A7%8D%E6%96%B9%E6%B3%95/" rel="next" title="Javascript 异步编程的 4 种方法">
      Javascript 异步编程的 4 种方法 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#prototype-属性的作用"><span class="nav-number">1.</span> <span class="nav-text">prototype 属性的作用</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#原型链"><span class="nav-number">2.</span> <span class="nav-text">原型链</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#constructor-属性"><span class="nav-number">3.</span> <span class="nav-text">constructor 属性</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Albert Guo"
      src="https://inews.gtimg.com/newsapp_ls/0/12122532695/0">
  <p class="site-author-name" itemprop="name">Albert Guo</p>
  <div class="site-description" itemprop="description">郭鹏松的个人博客</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives">
          <span class="site-state-item-count">19</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="mailto:albert-guo@outlook.com" title="E-Mail → mailto:albert-guo@outlook.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Albert Guo</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
